import re

from django_redis import get_redis_connection
from rest_framework import serializers
from rest_framework_jwt.settings import api_settings

from users.models import User


class CreateUserSerializer(serializers.ModelSerializer):
    # 因为我们没有直接定义模型类的字段,而是直接继承了django 自带的模型类
    # 所以我们自己定义这些属性
    password2 = serializers.CharField(label='确认密码',write_only=True)
    sms_code = serializers.CharField(label='短信验证码',write_only=True)
    allow = serializers.CharField(label='同意协议',write_only=True)
    token = serializers.CharField(label='JWT token',read_only=True)

    class Meta:
        model = User
        fields = ('id','username','password','password2','sms_code','mobile','allow','token')
        extra_kwargs = {
            'username': {
                'min_length': 5,
                'max_length': 20,
                'error_messages': {
                    'min_length': '仅允许5-20个字符的用户名',
                    'max_length': '仅允许5-20个字符的用户名',
                }
            },
            'password': {
                'write_only': True,
                'min_length': 8,
                'max_length': 20,
                'error_messages': {
                    'min_length': '仅允许8-20个字符的密码',
                    'max_length': '仅允许8-20个字符的密码',
                }
            }
        }
    def validate_mobile(self,value):
        """验证手机号"""
        if not re.match(r'^1[3-9]\d{9}$', value):
            raise serializers.ValidationError('手机号格式错误')
        return value

    def validate_allow(self,value):
        if value != 'true':
            raise serializers.ValidationError('请勾选同意选项')

    def validate(self, data):
        if data["password"] != data["password2"]:
            raise serializers.ValidationError('两次密码不一样')

        mobile = data["mobile"]
        redis_conn = get_redis_connection('verify_codes')
        real_redis_data = redis_conn.get("sms_%s" % mobile)
        if not real_redis_data:
            raise serializers.ValidationError('无效短信验证码')
        if real_redis_data.decode() != data["sms_code"]:
            raise serializers.ValidationError('短信验证码错误')

        return data

    def create(self,validated_data):
        """重写保存方法,增加密码加密"""

        # 移除数据库模型类中不需要的属性
        del validated_data['password2']
        del validated_data['sms_code']
        del validated_data['allow']

        user = super().create(validated_data)
        # user = User.objects.create(**validated_data)
        user.set_password(validated_data['password'])
        user.save()


        # 注册后签发JWT
        jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
        jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER

        payload = jwt_payload_handler(user)
        token = jwt_encode_handler(payload)
        user.token = token

        return user